import * as api from '../../utils/api.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    Images:{},
    details: {},
    // branch_mobile: '', // 分公司电话
    customer_mobile: '', // 提货商家电话
    fileList: [],
    order_sn:''
  },
  onDetails(order_sn) {
    let app=this
    api.workerRepairOrderDetail(order_sn).then(res => {
      app.setData({
        Images:res.data.repair_order.image_arr,
        details: res.data,
        customer_mobile: res.data.customer_mobile,
        fileList:res.data.repair_order.worker_images
      })
    })
  },

  callPhone(e) {
    const mobile = e.currentTarget.dataset.mobile
    if (mobile) {
      wx.makePhoneCall({
        phoneNumber: mobile
      })
    } else {
      wx.showToast({
        title: '未设置电话',
        icon: 'error'
      })
    }
  },
  Images: function (e) {  
    console.log(e)
		var current=e.target.dataset.src;
		wx.previewImage({
		  	current: current, // 当前显示图片的http链接
		  	urls: this.data.Images // 需要预览的图片http链接列表
		})
	},

  afterRead(event) {
    const that = this
    const { file } = event.detail;
    const order_sn = that.data.order_sn
    // console.log(file)
    // return
    api.upload(file.url)
      .then(res => {
        let fileList = this.data.fileList
        const url = res.data.url
        fileList.push({ ...file, url })
        that.setData({
          fileList
        })
        api.workerRepairOrderUploadImages(order_sn,url)
        .then(res => {
          that.onLoad()
        })
        .catch(err => console.log(err))
      }).catch(err => {
        console.log(err)
      })
  },
  delFile(event) {
    let app = this
    let order_sn = app.data.order_sn
    let index = event.detail.index
    let fileList = this.data.fileList
    fileList.splice(index, 1)
    this.setData({fileList})
    api.workerRepairOrderDelImages(order_sn,index)
        .then(res => {
          app.onLoad()
        })
        .catch(err => console.log(err))
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.onDetails(options.order_sn)
    this.setData({
      order_sn:options.order_sn
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})